package cn.com.customer.manager.base

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.fragment.app.Fragment
import cn.com.customer.manager.R
import com.xuexiang.xui.widget.actionbar.TitleBar
import com.xuexiang.xui.widget.actionbar.TitleUtils
import com.xuexiang.xui.widget.dialog.LoadingDialog
import com.xuexiang.xutil.tip.ToastUtils


/**
 */
abstract class BaseFragment : Fragment(), View.OnClickListener {
    /**
     * 上下文实例
     */
    protected lateinit var mContext: Context
    private var progressDialog: LoadingDialog? = null
    private lateinit var titleBar: TitleBar
    protected var mBundle: Bundle? = null
    protected lateinit var mInflater: LayoutInflater

    /**
     * 获取布局layout的资源ID
     *
     * @return 资源ID
     */
    protected abstract fun getLayoutId(): Int

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBundle = arguments
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mInflater = inflater
        onBeforeCreateView()
        return inflater.inflate(getLayoutId(), container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if (savedInstanceState != null) {
            onRestartInstance(savedInstanceState)
        }

        initView()
    }

    /**
     * 在创建View前的操作
     */
    protected open fun onBeforeCreateView() {
    }

    /**
     * 初始化控件(必须实现此方法)
     */
    protected abstract fun initView()

    /**
     * 加载数据
     */
    protected open fun initData() {
    }

    override fun onClick(v: View?) {
    }


    /**
     * 初始化ToolBar
     */
    protected fun initActionBar( titlebar:TitleBar,name: String, isShowLeft: Boolean = false) {
        titleBar= TitleUtils.initTitleBar(activity, R.id.titlebar, name)
        if(isShowLeft){
            titleBar.setLeftImageResource(R.mipmap.icon_back_black).setLeftClickListener {
               activity?.finish()
            }
        }

    }
    open fun getTitleBar(): TitleBar {
        return titleBar;
    }
    override fun onDestroyView() {
        super.onDestroyView()

        mBundle = null

        if (progressDialog != null) {
            dismissProgressDialog()
            progressDialog = null
        }
    }

    protected open fun onRestartInstance(bundle: Bundle) {
    }


    /**
     * 隐藏对话框
     */
    protected fun dismissProgressDialog() {
        if (progressDialog != null && progressDialog!!.isShowing) {
            progressDialog!!.dismiss()
        }
    }


}

